package cn.zl.ssoserver.util;


import cn.zl.ssoserver.constant.ServerConstant;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description:SsoCookie服务工具类
 * @Author: zllwsy
 * @Date: 2020/11/4 10:54
 */
public class SsoCookieUtil {
    /**
     *
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    /**
     *
     * @param response
     * @param cookieName
     * @param value
     * @param maxAge
     */
    public static void setCookie(HttpServletResponse response, String cookieName, String value,int maxAge) {
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

    /**
     * 设置doMainCookie
     * @param response
     * @param cookieName
     * @param value
     * @param maxAge
     */
    public static void setDomainCookie(HttpServletResponse response,String cookieName,String value,int maxAge){
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setDomain(ServerConstant.COOKIE_DOMAIN);
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
}
